/*************************************************************************
	> File Name: exec.c
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/08/25 09:00
 ************************************************************************/

#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

const char *myfile = "/home/halil/Documents/code/learning-of-operating-system/Procecss/DoMySelfProgram/mycmd";

int main(int argc, char *argv[], char *env[]) {
    pid_t id = fork();
    char *const _env[] = {
        "MY_VAL=8899",
        NULL
    };
    if (id < 0) {
        perror("fork");
        exit(-1);
    } else if (id == 0) {
        printf("Child process running, pid:%d\n", getpid());
        char *const _argv[] = {
            "mycmd",
            "-b",
            NULL
        };
        execve(myfile, _argv, env);
//        execlp("bash", "bash", "test.sh", NULL);
//        execlp("python", "python", "test.py", NULL);
//        execv(myfile, _argv);
        exit(1);
    } else {
        printf("Father process running, pid:%d\n", getpid());
        int status = 0;
        pid_t res = waitpid(-1, &status, 0);
        if (id > 0) printf("Wait child process success, exit code:%d\n", WEXITSTATUS(status));
    }
    return 0;
}

